// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:html';

import 'package:web_ui/web_ui.dart';

/**
 * Primitive tabbed pane WebComponent.
 */
class Tabs extends WebComponent {
  /** List of tab bodies */
  final tabs = <Element>[];

  /** Currently active tab */
  Element _activeTab;

  inserted() {
    tabs.addAll(_root.queryAll("[data-title]"));

    if (!tabs.isEmpty) activeTab = tabs[0];
  }

  get activeTab => _activeTab;
  set activeTab (tab) {
    _activeTab = tab;
    tabs.forEach((pane) => pane.style.display = _displayStyle(pane));
  }

  /** Switch to the tab that has `data-href` equal to the given [href]. */
  switchTo(href) {
    for (var tab in tabs) {
      if (tab.attributes['data-href'] == href) {
        activeTab = tab;
        break;
      }
    }
  }

  /** Compute class for the given tab [pane] */
  _class(pane) => (pane == activeTab) ? "active" : "";

  /** Compute display property value for the given tab [pane] */
  _displayStyle(pane) => (pane == activeTab) ? "block" : "none";
}
